package dao;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Dao<T> {
	private Class<T> clazz;

	private Session session;

	public Dao(Session s, Class<T> clazz) {
		this.session = s;
		this.clazz = clazz;
	}

	@SuppressWarnings("unchecked")
	public T load(long id) {
		Transaction tx = session.beginTransaction();
		T t = (T) session.load(clazz, id);
		tx.commit();
		return t;
	}

	public T busca(String id) {
		Transaction tx = session.beginTransaction();
		@SuppressWarnings("unchecked")
		T t = (T) session.load(clazz, id);
		tx.commit();
		return t;
	}

	public void save(T t) {
		Transaction tx = session.beginTransaction();
		session.save(t);
		tx.commit();
	}

	public void delete(T t) {
		Transaction tx = session.beginTransaction();
		session.delete(t);
		tx.commit();
	}

	public void merge(T t) {
		Transaction tx = session.beginTransaction();
		session.merge(t);
		tx.commit();
	}

	public void update(T t) {
		Transaction tx = session.beginTransaction();
		session.update(t);
		tx.commit();
	}

	@SuppressWarnings("unchecked")
	public List<T> list() {
		Transaction tx = session.beginTransaction();

		@SuppressWarnings("rawtypes")
		List objts = null;
		Criteria selectAll = session.createCriteria(clazz);
		tx.commit();
		objts = selectAll.list();
		return objts;
	}
}